package initialize

import (
	"fmt"
	"gitee.com/swsk33/server-demo/config"
	"github.com/spf13/viper"
	"os"
)

// InitViper 用于初始化Viper及其配置读取
func InitViper() {
	viper.SetConfigName("config")
	viper.SetConfigType("yaml")
	viper.AddConfigPath(".")
	err := viper.ReadInConfig()
	if err != nil {
		fmt.Println("读取配置文件出错！", err)
		os.Exit(1)
	}
}

// GetDatabaseConfig 获取数据库配置
func GetDatabaseConfig() *config.DatabaseConfig {
	return &config.DatabaseConfig{
		Host:     viper.GetString("database.host"),
		Port:     viper.GetInt("database.port"),
		Database: viper.GetString("database.database"),
		Username: viper.GetString("database.username"),
		Password: viper.GetString("database.password"),
	}
}

// GetServerConfig 获取Web服务器配置
func GetServerConfig() *config.ServerConfig {
	return &config.ServerConfig{
		Port: viper.GetInt("server.port"),
	}
}